

const path = require('path');
const url = require('url');

module.exports = (appInfo) => {
    const config = {};

    config.proxy = true;

    // use for cookie sign key, should change to your own and keep security
    config.keys = `${appInfo.name}_1507686652732_7526`;

    // add your config here
    config.middleware = [];
    config.baiduTongjiCode = process.env.BAIDU_TONGJI_CODE;

    config.store = {
        loginName: process.env.STORE_LOGINNAME,
        hospitalId: process.env.STORE_HOSPITALID,
    }

    config.insurance = {
        path: process.env.INSURANCE_DOWNLOAD_PATH,
    }

    config.auth = {
        client: {
            match: { path: '/', end: false },
            exclude: ['/user/changePassword'],
            type: 'oauth-code',
            clientId: 'crm',
            clientSecret: 'crm_secret',
            scope: 'CRM',
            loginPath: '/user/login',
            redirectPath: '/user/redirect',
            logoutPath: '/user/logout',
            authorizationUri: process.env.OAUTH_WEB_URL
                ? url.resolve(process.env.OAUTH_WEB_URL, '/oauth/authorize')
                : 'eureka://oauth2-services/oauth/authorize',
            accessTokenUri: 'eureka://oauth2-services/oauth/token',
            logoutUri: process.env.OAUTH_WEB_URL
                ? url.resolve(process.env.OAUTH_WEB_URL, '/login/out')
                : 'eureka://oauth2-services/login/out',
            userInfoUri: 'eureka://oauth2-services/user/info',
            userIdGetter: 'data.user',
            userNameGetter: 'data.username',
            unauthorizedBehavior: (ctx) => {
                if (ctx.path.startsWith('/_api')) {
                    ctx.throw(401);
                } else {
                    ctx.redirect(`/user/login?r=${encodeURIComponent(ctx.originalUrl)}`);
                }
            },
        },
    };

    config.api = {
        clients: {
            user: {
                baseUrl: 'eureka://oauth2-services/',
                auth: 'transfer',
            },
            old: {
                baseUrl: 'eureka://wanhuhealth-old/',
                auth: 'transfer',
            },
            task: {
                baseUrl: 'eureka://wanhuhealth-task-server/',
                auth: 'transfer',
            },
            weixin: {
                baseUrl: 'eureka://wanhuhealth-weixin-bind/',
                auth: 'transfer',
            },
            portal: {
                baseUrl: 'eureka://wanhuhealth-crm-portal/',
                auth: 'transfer',
            },
            portalJson: {
                baseUrl: 'eureka://wanhuhealth-crm-portal/',
                auth: 'transfer',
                contentType: 'json',
            },
            doctor: {
                baseUrl: 'eureka://wanhuhealth-doctor-portal/',
                auth: 'transfer',
            },
            doctorJson: {
                baseUrl: 'eureka://wanhuhealth-doctor-portal/',
                auth: 'transfer',
                contentType: 'json',
            },
            aliyunOauth: {
                baseUrl: 'https://oauth.aliyun.com/v1/',
                validateResponse: async (resp) => {
                    if (resp.status < 200 || resp.status > 299) {
                        const e = new Error(`网络错误：${resp.status} ${resp.statusText || 'Unknown'}`);
                        e.status = resp.status;
                        e.data = await resp.json();
                        throw e;
                    }
                },
                transformResponse: data => data,
            },
            aliyunProxy: {
                baseUrl: 'eureka://wanhuhealth-crm-portal/cloudcallcenter/',
                auth: 'transfer',
                contentType: 'json',
            },
        },
    };

    config.view = {
        client: {
            path: path.resolve(__dirname, '..', '..', 'client', 'build'),
            oss: process.env.CLIENT_STATIC_OSS_URL
                ? { url: process.env.CLIENT_STATIC_OSS_URL }
                : null,
        },
    };

    config.oss = {
        accessKeyId: process.env.OSS_KEY_ID,
        accessKeySecret: process.env.OSS_KEY_SECRET,
        bucket: process.env.OSS_BUCKET,
        region: process.env.OSS_REGION,
        prefix: 'gpmentor-web/media/', /* must have suffix slash(/) or nothing */
    };

    config.call = {
        clientId: process.env.CALL_CLIENT_ID,
        clientSecret: process.env.CALL_CLIENT_SECRET,
        clientInstanceId: process.env.CALL_INSTANCE_ID,
    };

    config.security = {
        csrf: false,
    };

    config.bodyOnDemand = {
        formLimit: '4mb',
    }

    config.release = process.env.APPLICATION_VERSION;

    return config;
};
